package com.hxd.otc.utils;

import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;

import com.hxd.otc.BuildConfig;

/**
 * 类  名 :  LogUtils
 * 作  者 :  胡庆岭
 * 时  间 :  2018/4/24 18:12
 * 描  述 :  ${Log工具类}
 */
public class LogUtils {

    private static final String TAG = "OTC";

    public static final boolean DEBUG = BuildConfig.isDebug; //调试模式

    private static int LOG_MAXLENGTH = 2000;

    public static void e(Throwable throwable) {
        if (DEBUG) {
            Log.e(TAG, throwable.getMessage());
        }
    }


    public static void v(String msg) {
        v(TAG, msg);
    }

    public static void v(String tagName, String msg) {
        if (TextUtils.isEmpty(msg))
            return;
        if (DEBUG) {
            int strLength = msg.length();
            int start = 0;
            int end = LOG_MAXLENGTH;
            for (int i = 0; i < 100; i++) {
                if (strLength > end) {
                    Log.v(tagName + i, msg.substring(start, end));
                    start = end;
                    end = end + LOG_MAXLENGTH;
                } else {
                    Log.v(tagName + i, msg.substring(start, strLength));
                    break;
                }
            }
        }
    }

    public static void d(String msg) {
        d(TAG, msg);
    }

    public static void d(String tagName, String msg) {
        if (TextUtils.isEmpty(msg))
            return;
        if (DEBUG) {
            int strLength = msg.length();
            int start = 0;
            int end = LOG_MAXLENGTH;
            for (int i = 0; i < 100; i++) {
                if (strLength > end) {
                    Log.d(tagName + i, msg.substring(start, end));
                    start = end;
                    end = end + LOG_MAXLENGTH;
                } else {
                    Log.d(tagName + i, msg.substring(start, strLength));
                    break;
                }
            }
        }
    }

    public static void i(String msg) {
        i(TAG, msg);
    }

    public static void i(String tagName, String msg) {
        if (TextUtils.isEmpty(msg))
            return;
        if (DEBUG) {
            int strLength = msg.length();
            int start = 0;
            int end = LOG_MAXLENGTH;
            for (int i = 0; i < 100; i++) {
                if (strLength > end) {
                    Log.i(tagName + i, msg.substring(start, end));
                    start = end;
                    end = end + LOG_MAXLENGTH;
                } else {
                    Log.i(tagName + i, msg.substring(start, strLength));
                    break;
                }
            }
        }
    }

    public static void w(String msg) {
        w(TAG, msg);
    }

    public static void w(String tagName, @NonNull String msg) {
        if (TextUtils.isEmpty(msg))
            return;
        if (DEBUG) {
            int strLength = msg.length();
            int start = 0;
            int end = LOG_MAXLENGTH;
            for (int i = 0; i < 100; i++) {
                if (strLength > end) {
                    Log.w(tagName + i, msg.substring(start, end));
                    start = end;
                    end = end + LOG_MAXLENGTH;
                } else {
                    Log.w(tagName + i, msg.substring(start, strLength));
                    break;
                }
            }
        }
    }

    public static void e(String msg) {
        e(TAG, msg);
    }

    public static void e(String tagName, String msg) {
        if (TextUtils.isEmpty(msg))
            return;
        if (DEBUG) {
            int strLength = msg.length();
            int start = 0;
            int end = LOG_MAXLENGTH;
            for (int i = 0; i < 100; i++) {
                if (strLength > end) {
                    Log.e(tagName + i, msg.substring(start, end));
                    start = end;
                    end = end + LOG_MAXLENGTH;
                } else {
                    Log.e(tagName + i, msg.substring(start, strLength));
                    break;
                }
            }
        }
    }
}
